package com.niit.mall.common.api;

import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;

import java.util.List;

/**
 * @BelongsProject: pardo_mall_backend
 * @BelongsPackage: com.niit.mall.common.api
 * @Author: ChuXuan
 * @CreateTime: 2025-05-23  17:47
 * @Description: 通用分页数据
 * @Version: 1.0
 */

public class CommonPage<T> {
	/**
	 * 当前页码
	 */
	private Integer pageNum;
	/**
	 * 每页数量
	 */
	private Integer pageSize;
	/**
	 * 总页数
	 */
	private Integer totalPage;
	/**
	 * 总条数
	 */
	private Long total;
	/**
	 * 分页数据
	 */
	private List<T> list;
	
	/**
	 * 将PageHelper分页后的list转为分页信息
	 */
	public static <T> CommonPage<T> restPage(List<T> list) {
		CommonPage<T> result = new CommonPage<T>();
		PageInfo<T> pageInfo = new PageInfo<T>(list);
		result.setTotalPage(pageInfo.getPages());
		result.setPageNum(pageInfo.getPageNum());
		result.setPageSize(pageInfo.getPageSize());
		result.setTotal(pageInfo.getTotal());
		result.setList(pageInfo.getList());
		return result;
	}
	
	/**
	 * 将SpringData分页后的list转为分页信息
	 */
	public static <T> CommonPage<T> restPage(Page<T> pageInfo) {
		CommonPage<T> result = new CommonPage<T>();
		result.setTotalPage(pageInfo.getTotalPages());
		result.setPageNum(pageInfo.getNumber());
		result.setPageSize(pageInfo.getSize());
		result.setTotal(pageInfo.getTotalElements());
		result.setList(pageInfo.getContent());
		return result;
	}
	
	public Integer getPageNum() {
		return pageNum;
	}
	
	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}
	
	public Integer getPageSize() {
		return pageSize;
	}
	
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	
	public Integer getTotalPage() {
		return totalPage;
	}
	
	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}
	
	public List<T> getList() {
		return list;
	}
	
	public void setList(List<T> list) {
		this.list = list;
	}
	
	public Long getTotal() {
		return total;
	}
	
	public void setTotal(Long total) {
		this.total = total;
	}
}
